package com.ad.pet.app.main.desniger;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ad.pet.DaggerMainActivityCompent;
import com.ad.pet.R;
import com.ad.pet.app.Navigation;
import com.ad.pet.app.main.desniger.adapter.DesnigerShopListAdapter;
import com.ad.pet.common.data.utils.ui.ToastUtil;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.activity.LoadingActivity;
import com.ad.pet.models.remote.login.Country;
import com.ad.pet.models.remote.main.DesingerMain;
import com.ad.pet.models.remote.main.DesingerStore;
import com.ad.pet.models.remote.main.DesingerType;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by SEELE on 2019/6/27.
 */

public class DesnigerStoreListActivity extends LoadingActivity implements DesnigerContract.View {


    @Inject
    DesnigerPresenter mDesnigerPresenter;

    DesingerStore desingerStore;

    int page = 1;
    @BindView(R.id.recycler_home)
    RecyclerView mRecyclerHome;
//    @BindView(R.id.swipeRefresh)
//    SwipeRefreshLayout mSwipeRefresh;

    @BindView(R.id.tv_city)
    TextView mTvCity;

    @BindView(R.id.et_search)
    EditText mEtSearch;
    @BindView(R.id.iv_close)
    ImageView mIvClose;

    String myLong;
    String mylat;

    private DesnigerShopListAdapter mDesnigerShopListAdapter;
    private Country country;


    @Override
    protected void initViews() {

        mDesnigerPresenter.onCreate(this);
        mStatusLayoutManager.showContent();

        desingerStore = (DesingerStore) getIntent().getSerializableExtra("Store");
        mDesnigerShopListAdapter = new DesnigerShopListAdapter(null);
        mRecyclerHome.setLayoutManager(new LinearLayoutManager(mActivity));
        mRecyclerHome.setAdapter(mDesnigerShopListAdapter);
        mDesnigerShopListAdapter.bindToRecyclerView(mRecyclerHome);
        myLong = getIntent().getStringExtra("long");
        mylat = getIntent().getStringExtra("lan");

        mTvCity.setText(desingerStore.getCity_name());
        country = new Country();
        country.setCity_id(desingerStore.getCity_id());
        country.setCity_name(desingerStore.getCity_name());
        country.setProvince_id(desingerStore.getProvince_id());
//
//        mDesnigerPresenter.getDesginerShop(String.valueOf(page), "10");
    }

    @Override
    protected void initData() {

        mDesnigerPresenter.onCreate(this);
        mDesnigerPresenter.getDesginerShop("1", "30", myLong, mylat, "", desingerStore.getProvince_id(), desingerStore.getCity_id());
    }

    @Override
    protected void initEvents() {
        mTvCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.goChoseCountry(mActivity, 100);
            }
        });

        mEtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (TextUtils.isEmpty(mEtSearch.getText().toString().trim())) {
                    ToastUtil.getToastUtil().showShort(getString(R.string.please_input_serach_content));
                    return false;
                }
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    mDesnigerPresenter.getDesginerShop("1", "30", myLong, mylat, mEtSearch.getText().toString(), desingerStore.getProvince_id(), desingerStore.getCity_id());
                    return true;
                }
                return false;
            }
        });

        mIvClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(mEtSearch.getText().toString())) {
                    mEtSearch.setText("");
                    mDesnigerPresenter.getDesginerShop("1", "30", myLong, mylat, "", desingerStore.getProvince_id(), desingerStore.getCity_id());
                }
            }
        });
//        mDesnigerShopListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
//            @Override
//            public void onLoadMoreRequested() {
//                page++;
//                mDesnigerPresenter.getDesginerShop(String.valueOf(page), "10");
//            }
//        }, mRecyclerHome);
//        mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                mSwipeRefresh.setRefreshing(true);
//                page = 1;
//                mDesnigerPresenter.getDesginerShop(String.valueOf(page), "10");
//            }
//        });
        mDesnigerShopListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                DesingerMain desingerMain = mDesnigerShopListAdapter.getItem(position);
                if (desingerMain.getIs_redecorate() == 0) {
//                    desingerMain.setCity_name(country.getCity_name());
//                    desingerMain.setCity_id(country.getCity_id());
//                    desingerMain.setPrivoince_id(country.getProvince_id());
//
//
//                    RxBus.getDefault().post(desingerMain);
//                    finish();
                } else {
                    ToastUtil.getToastUtil().showShort("门店装修中，敬请期待");
                }

//                Navigation.goDesignerStoreList(mActivity,mDesnigerShopListAdapter.getData().get(position).getStore_id(),mDesnigerShopListAdapter.getData().get(position).getStore_name(),MainActivity.REQUEST_GO_MALL);
            }
        });
    }

    @Override
    public void initInject() {
        DaggerMainActivityCompent.builder().applicationComponent(getApplicationComponent()).activityModule(getActivityModule()).build().inject(this);
    }


    @Override
    protected int contentLayout() {
        return R.layout.acitivity_desniger_store_list;
    }

    @Override
    protected void clickRetry(View v) {

    }

    @Override
    protected BasePresenter getPresenter() {
        return mDesnigerPresenter;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void error(int code, String msg) {

        ToastUtil.getToastUtil().showShort(msg);
    }


    @Override
    public void getDesginerShopSuccess(List<DesingerMain> stores) {
//        mSwipeRefresh.setRefreshing(false);


//        if (page == 1) {
//            mDesnigerShopListAdapter.setNewData(store.getStoreList());
//        } else {
//            mDesnigerShopListAdapter.addData(store.getStoreList());
//            mDesnigerShopListAdapter.loadMoreComplete();
//        }
//
//        if (store.getStoreList() != null && store.getStoreList().size() < 10) {//CommonConstant.PAGE_COUNT
//            //暂无更多数据
//            mDesnigerShopListAdapter.loadMoreEnd();
//        }
        if (page == 1) {
            mDesnigerShopListAdapter.setNewData(stores);
        }
        if (stores == null || stores.size() == 0 && page == 1) {
//            mDesnigerShopListAdapter.bindToRecyclerView(mRecyclerHome);
            mDesnigerShopListAdapter.setEmptyView(R.layout.layout_empty_address);
            ((TextView) mDesnigerShopListAdapter.getEmptyView().findViewById(R.id.tv_title)).setText("暂无门店~");
        }

    }

    @Override
    public void getShopServiceSuccess(List<DesingerType> shopServices) {

    }

    @Override
    public void getStoreDetailSuccess(DesingerMain desingerMain) {

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null)
            return;
        switch (requestCode) {
//            case MainActivity.REQUEST_GO_MALL:
//                setResult(RESULT_OK, data);
//                finish();
//                break;
            case 100:
                country = (Country) data.getSerializableExtra("country");
                mTvCity.setText(country.getCity_name());
                mDesnigerPresenter.getDesginerShop("1", "30", myLong, mylat, "", country.getProvince_id(), country.getCity_id());
                break;
        }
    }


}
